﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace wljHomoWork2WS
{
    class wljMain
    {
        public void Main1()
        {
            string url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
            StringBuilder sb = null;
            try
            {
                //根据指定的URL模拟一个网络请求
                HttpWebRequest wrqPage = HttpWebRequest.Create(url) as HttpWebRequest;
                //设定UserAgent的属性
                wrqPage.UserAgent = "Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1)";
                //获取网络相应
                WebResponse wrsPage = wrqPage.GetResponse();
                //获取相应数据流
                Stream strmPage = wrsPage.GetResponseStream();
                //根据数据流创建XmlReader对象
                XmlReader _XmlReader = XmlReader.Create(strmPage);
                //创建XmlDoc对象
                XmlDocument _Doc = new XmlDocument();
                _Doc.Load(_XmlReader);
                XmlNamespaceManager _XmlNamespaceManager = new XmlNamespaceManager(_Doc.NameTable);
                _XmlNamespaceManager.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
                XmlNodeList _NodeList = _Doc.SelectNodes("//wsdl:message", _XmlNamespaceManager);

            }
            catch(Exception Ex)
            {
                Console.WriteLine(Ex.Message);
            }

        }
        static void Main(string[] args)
        {
            //定义Web服务Url
            string url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
            StringBuilder sb = null;
            //实例化WljWs2
            wljWS2 _wljWs2 = new wljWS2();
            _wljWs2.Uri = url;
            //选择message
            XmlNodeList _NodeList = _wljWs2.ReturnNodesByElement("//wsdl:message", "wsdl", "http://schemas.xmlsoap.org/wsdl/");
            sb = new StringBuilder();
            sb.AppendFormat("message元素总共有{0}个,它们的Name属性分别为:\n", _NodeList.Count);
            int No = 1;//定义序号
            foreach (XmlNode _Node in _NodeList)
            {
                sb.AppendFormat("{0}、{1}\n",No++,_Node.Attributes[0].Value);
            }


            No = 1;
            //选择operation element of binding
            _NodeList = _wljWs2.ReturnNodesByElement("//wsdl:binding/wsdl:operation", "wsdl", "http://schemas.xmlsoap.org/wsdl/");
            sb.AppendFormat("\nThe count operation element of binding element is {0},and their names are:\n",_NodeList.Count);
            foreach (XmlNode _Node in _NodeList)
            {
                sb.AppendFormat("{0}、{1}\n", No++, _Node.Attributes[0].Value);
            }
            No = 1;

            url = "http://webservice.webxml.com.cn/WebServices/ValidateEmailWebService.asmx?wsdl";
            _wljWs2.Uri = url;
            _NodeList = _wljWs2.ReturnNodesByElement("//s:complexType", "s", "http://www.w3.org/2001/XMLSchema");
            sb.AppendFormat("\nThe count complex type element of binding element is {0},and their names are:\n", _NodeList.Count);
            foreach (XmlNode _Node in _NodeList)
            {
                sb.AppendFormat("{0}、{1}\n", No++, _Node.ParentNode.Attributes["name"].Value);
            }
            No = 1;
            _NodeList = _wljWs2.ReturnNodesByElement("//wsdl:portType/wsdl:operation", "wsdl", "http://schemas.xmlsoap.org/wsdl/");
            sb.AppendFormat("\nThe count of operation elements in portType  element is {0},and their names are:\n", _NodeList.Count);
            foreach (XmlNode _Node in _NodeList)
            {
                sb.AppendFormat("{0}、{1}\n", No++, _Node.Attributes["name"].Value);
            }
            No = 1;
            _NodeList = _wljWs2.ReturnNodesByElement("//wsdl:service", "wsdl", "http://schemas.xmlsoap.org/wsdl/");
            sb.Append("\nservice element and its sub-elements are:");
            foreach (XmlNode _Node in _NodeList)
            {
                sb.Append("\n"+_Node.OuterXml);
            };
            
            Console.WriteLine(sb.ToString());
            //结果输出路径
            string path = System.Environment.CurrentDirectory+@"\output(wlj).txt";
            try
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                using (FileStream fs = File.Create(path))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
                    fs.Write(info,0,info.Length);
                }
                Console.WriteLine("结构以输出到Bin目录下的output.txt中,按任意键结束程序");
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
                Console.WriteLine("按任意键结束程序");
            }
            Console.ReadKey();
        }
    }
}
